package com.rabbitmq.rabbitmqextensions.listener;

import com.rabbitmq.client.Channel;
import com.rabbitmq.rabbitmqextensions.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

//@Component
@Slf4j
public class AckListener {

//    @RabbitListener(queues = Constants.ACK_QUEUE)
//    public void listener(Message message, Channel channel) throws IOException {
//
//        long deliveryTag = message.getMessageProperties()
//                .getDeliveryTag();
//
//        int num = 1 / 0;
//
//        log.info("接收消息: {}; deliveryTag: {}", new String(message.getBody()), deliveryTag);
//
//    }

    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void listener(Message message, Channel channel) throws IOException {

        long deliveryTag = message.getMessageProperties()
                .getDeliveryTag();

        try {

            log.info("接收消息: {}; deliveryTag: {}", new String(message.getBody()), deliveryTag);

            int n = 1 / 0;

            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {

            channel.basicNack(deliveryTag, false, true);
        }

    }
}
